package com.yulinlin.jdbc.mysql;

import com.yulinlin.core.wrapper.IInsertWrapper;
import com.yulinlin.core.wrapper.IParamsWrapper;

import java.util.ArrayList;
import java.util.List;

public class MysqlInsertWrapper implements IInsertWrapper {

    private IParamsWrapper params;

    private String tableName;

    private List<String[]> columns;


    public MysqlInsertWrapper() {
        this.params = new MysqlParamsWrapper();
        this.columns = new ArrayList<>();
    }

    @Override
    public IInsertWrapper column(String name, Object params) {
        String value =  this.params.push(params);
        this.columns.add(new String[]{
                name,value
        });
        return this;
    }

    @Override
    public IInsertWrapper table(String tableName) {
        this.tableName = tableName;
        return this;
    }

    @Override
    public String getSql() {
        StringBuffer sb =  new StringBuffer();
        sb.append("insert into ");
        sb.append(tableName);

        StringBuffer columns = new StringBuffer();
        StringBuffer values = new StringBuffer();


        for (String[] column:this.columns){
            if(columns.length() > 0){
                columns.append(" , ");
                values.append(" , ");
            }
            columns.append(column[0]);
            values.append(column[1]);
        }

        sb.append(" ( "+columns+" ) ");
        sb.append(" values ");
        sb.append(" ( "+values+" ) ");
        return sb.toString();
    }

    @Override
    public IParamsWrapper getParams() {
        return this.params;
    }
}
